/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class FullScreenFrame extends JFrame {
	public FullScreenFrame(String title) {
		super(title);

		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gs = ge.getDefaultScreenDevice();

		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setUndecorated(true);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setBounds(0, 0, screenSize.width, screenSize.height);

		try {
			gs.setFullScreenWindow(this);
			DisplayMode dm = gs.getDisplayMode();
			this.setVisible(true);
		} catch (Exception e) {
			System.err.println("Exception: " + e);
			gs.setFullScreenWindow(null);
		}

	}

	public void addArea(Canvas panel) {
		this.add(panel);
	}

	public static void main(String[] args) {

		// bug happens with both native and cross-plaform (Metal) look and
		// feels.

		// String laf = UIManager.getSystemLookAndFeelClassName();
		String laf = UIManager.getCrossPlatformLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(laf);
		} catch (Exception e) {
		}

		final JFrame frame = new JFrame("FullScreenTest");
		frame.getContentPane().setLayout(new FlowLayout());
		frame.getContentPane().add(new JComboBox(new String[] { "One", "Two", "Three" }));
		JButton quit = new JButton("Quit");
		quit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
		frame.getContentPane().add(quit);
		frame.setUndecorated(true);

		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gs = ge.getDefaultScreenDevice();

		try {
			gs.setFullScreenWindow(frame);
			DisplayMode dm = gs.getDisplayMode();
			frame.setVisible(true);
		} catch (Exception e) {
			System.err.println("Exception: " + e);
			gs.setFullScreenWindow(null);
		}

		while (frame.isVisible()) {
			try {
				Thread.currentThread();
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}

		gs.setFullScreenWindow(null);
	}
}
